package com.LeeCode;

/**
 * 分配给商店的最多商品的最小值
 */

public class Code2064 {
    public static void main(String[] args) {
        int[] quantities = {15, 10, 10};
        int n = 7;
        System.out.println(new Code2064().minimizedMaximum(n, quantities));
    }

    public int minimizedMaximum(int n, int[] quantities) {
        int left = 0, right = 0;
        for (int num : quantities) {
            right = Math.max(right, num);
        }
        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (check(quantities, n, mid))
                right = mid;
            else
                left = mid;
        }
        return right;
    }

    private boolean check(int[] quantities, int n, int limit) {
        int count = 0;
        for (int num : quantities) {
            count += (num + limit - 1) / limit;

            if (count > n)
                return false;
        }
        return count <= n;
    }
}
